﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace asp.netWebForms
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillData();
            }
        }

        private void FillData()
        {
            var sql = "select * from Articles";
            var data = DbHelper.GetData(sql);

            gv.DataSource = data;
            gv.DataBind();
        }

        private string GetValue(int rowIndex, int colIndex)
        {
            var c = gv.Rows[rowIndex].Cells[colIndex];
            var value = c.Controls.Count > 0 ? ((TextBox)c.Controls[0]).Text : c.Text;

            return value;
        }

        protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gv.EditIndex = -1;
            FillData();
        }

        protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var id = GetValue(e.RowIndex, 0);
            var d = string.Format("delete from Articles where id = {0}", id);

            DbHelper.ADU(d);
            gv.EditIndex = -1;
            FillData();
        }

        protected void gv_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gv.EditIndex = e.NewEditIndex;
            FillData();
        }

        protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var idString = GetValue(e.RowIndex, 0);
            var isOk = int.TryParse(idString, out int id);

            var title = GetValue(e.RowIndex, 1).Replace("&nbsp", "");
            var author = GetValue(e.RowIndex, 2).Replace("&nbsp", "");
            var content = GetValue(e.RowIndex, 3).Replace("&nbsp", "");
            var remarks = GetValue(e.RowIndex, 4).Replace("&nbsp", "");

            if (isOk)
            {
                var u = string.Format("update Articles set title ='{0}',author='{1}',content='{2}',remarks='{3}' where id={4}", title, author, content, remarks, id);

                DbHelper.ADU(u);
            }
            else
            {
                var a = string.Format("insert into Articles (author,title,content,remarks) values ('{0}','{1}','{2}','{3}')", author, title, content, remarks);

                DbHelper.ADU(a);
            }

            gv.EditIndex = -1;
            FillData();
        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add(new DataColumn("Id"));
            dt.Columns.Add(new DataColumn("Title"));
            dt.Columns.Add(new DataColumn("Author"));
            dt.Columns.Add(new DataColumn("Content"));
            dt.Columns.Add(new DataColumn("Remarks"));

            foreach (GridViewRow row in gv.Rows)
            {
                var newRow = dt.NewRow();

                newRow["Id"] = GetValue(row.RowIndex, 0);
                newRow["Title"] = GetValue(row.RowIndex, 1).Replace("&nbsp", "");
                newRow["Author"] = GetValue(row.RowIndex, 2).Replace("&nbsp", "");
                newRow["Content"] = GetValue(row.RowIndex, 3).Replace("&nbsp", "");
                newRow["Remarks"] = GetValue(row.RowIndex, 4).Replace("&nbsp", "");

                dt.Rows.Add(newRow);
            }

            dt.Rows.Add(dt.NewRow());
            gv.DataSource = dt;
            gv.DataBind();
            gv.EditIndex = dt.Rows.Count - 1;
            gv.DataSource = dt;
            gv.DataBind();
        }
    }
}